package cn.bearspark.api.service.func.impl;

import cn.bearspark.api.service.func.CollectService;
import cn.bearspark.article.rpc.ArticleRpc;
import cn.bearspark.collect.rpc.CollectRpc;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.common.enumeration.CollectedEnum;
import cn.bearspark.common.enumeration.ModuleEnum;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class CollectServiceImpl implements CollectService {

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private CollectRpc collectRpc;
    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private ArticleRpc articleRpc;

    @Override
    public void collectArticle(Long articleId, Integer collected) {
        long userId = BSRequestContext.getUserId();

        // 记录该用户收藏该专栏，如果记录失败，则直接返回
        if (!collectRpc.collect(userId, ModuleEnum.ARTICLE.getCode(), articleId, collected)) {
            return;
        }

        if (collected == CollectedEnum.COLLECTED.getCode()) {
            // 专栏的收藏数加一
            articleRpc.incrCollectNum(articleId);
        } else {
            // 专栏的收藏数减一
            articleRpc.decrCollectNum(articleId);
        }
    }
}
